package com.sunchangming.uniqname;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;

import com.google.protobuf.BlockingService;

/**
 * 2字节长度，1字节的typeid，byte[] body
 * @author cm
 *
 */
public class ProtobufCodecFactory implements ProtocolCodecFactory  {

	/**
	 * Instance of {@link ProtocolDecoder} that can deal with the Prtobuf wire format.
	 */
	private final ProtobufDecoder decoder;
	private final ProtocolEncoder encoder;
	
	/**
	 * Private constructor used by factory methods.
	 * 
	 * @param decoder Instance of {@link ProtobufDecoder}.
	 */
	public ProtobufCodecFactory( final BlockingService service,boolean isServer) {
		this.decoder = new ProtobufDecoder(service,isServer);
		this.encoder = new ProtobufEncoder();
	}
	
	/**
	 * @param session Not in use
	 * @return Instance of {@link ProtobufDecoder}
	 */
	@Override
	public ProtocolDecoder getDecoder(IoSession session) throws Exception {
		return decoder;
	}

	/**
	 * @param session Not in use
	 * @return Instance of {@link ProtobufEncoder}
	 */
	@Override
	public ProtocolEncoder getEncoder(IoSession session) throws Exception {
		return encoder;
	}	
	
}
